package com.xiaoluo.lottery.common.weixin.utils;

import java.io.IOException;
import java.security.KeyStore;
import java.security.KeyStoreException;

/**
 * Project : lottery
 * Package : main.java.com.xiaoluo.lottery.common.weixin.pay.utils
 * Created by Willian.
 * Date  : 2016/4/6 17:14.
 */
public class SSLUtil {

    private final static String CERT_FILE_NAME = "cert/apiclient_cert.p12";

    /**
     *
     * @param password
     * @return
     */
    public static KeyStore readKeyStore(String password) {
        KeyStore ks = null;
        try {
            ks = KeyStore.getInstance("PKCS12");
        } catch (KeyStoreException e) {
            e.printStackTrace();
        }

        // get user password and file input stream
        java.io.FileInputStream fis = null;
        try {
            String path = Configure.getCertLocalPath();

            if(Configure.getCertLocalPath() == null || "".equals(Configure.getCertLocalPath())){
                path = SSLUtil.class.getResource("/")+CERT_FILE_NAME;//"F:\\workspace2\\lottery\\target\\test-classes\\cert\\apiclient_cert.p12";
            }

            fis = new java.io.FileInputStream(path);
            ks.load(fis, password.toCharArray());

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return ks;
    }
}
